import java.applet.*;
import java.awt.*;

public class Main extends Applet implements Runnable
{
	private Graphics dbg;
	private Image dbImage;

	private Board theBoard;

	private Dimension appletSize = getSize();

	Thread th;

	//Called initially when the user enters the site
	public void init()
	{
		setBackground(Color.black);
		theBoard = new Board(this.getSize().width, this.getSize().height);

	}
	
	//This method is called everytime the user enters the site with the applet
	public void start()
	{
		th = new Thread(this);
		th.start();
	
	}

	//This method is called if the user leaves the site with the applet
	public void stop()
	{

	}

	//This method is called if the user leaves the page (such as closing the browser)
	public void destroy()
	{
	
	}

	//Main loop method
	public void run()
	{
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
	
		while(true)
		{
			
			repaint();

			try
			{
				Thread.sleep(20);
			}
			catch(InterruptedException ex)
			{
				// do nothing
			}
			
			Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
		}

	}

	//Used to draw things into the applet
	public void paint(Graphics g)
	{
//		g.setColor(Color.yellow);
//		g.drawString("I love Java Applets", 20, 20);			
		Board.drawBoard(g);
		
	}



	//This method is called when repaint() gets called or the browser moves
	//This is overriden to prevent flickering screen by implementing double buffering
	//
	//(Basically a generic method that never needs to be further modified. All additional 
	//code to draw images should be added to paint method)
	public void update(Graphics g)
	{
		if(dbImage == null)
		{
			dbImage = createImage(this.getSize().width, this.getSize().height);
			dbg = dbImage.getGraphics();
		}

		dbg.setColor (getBackground ());
		dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

		dbg.setColor (getForeground());
		paint (dbg);

		g.drawImage (dbImage, 0, 0, this);


	}







}

